//
//  HWPromoteController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/26.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import JXSegmentedView
import JXPagingView
class HWPromoteController: HWBaseViewController {
    
    var pagingView:JXPagingView!
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var titles:[String] = []
    var ids:[String] = []
    var topView:HWPromoteTopView!
    let httpManager = HWHttpManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
//        addRefresh()
        
        addAction()
        
        loadData()
        
    }
    
}

extension HWPromoteController {
    private func initUI() -> Void {
        
        navigationView.backgroundColor = .white
        navigationView.isHidden = false
        navigationView.titleLabel.text = "推广"
        navigationView.backButton.isHidden = true
        
        view.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        
        topView = HWPromoteTopView()
//        topView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        topView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 182*widthScale)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = titles
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#FFFFFF")
        segmentedDataSource.titleNormalFont = UIFont.boldSystemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = UIFont.boldSystemFont(ofSize: 14*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = true
        
        segmentedView.dataSource = segmentedDataSource
        let indicator = JXSegmentedIndicatorBackgroundView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#5559EA")
        
        segmentedView.indicators = [indicator]
        segmentedView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50*widthScale)
        segmentedView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")

        view.addSubview(self.segmentedView)
        
        pagingView = JXPagingView(delegate: self)
        pagingView.mainTableView.backgroundColor = .clear
        pagingView.listContainerView.isCategoryNestPagingEnabled = true
        view.addSubview(pagingView)
        
        segmentedView.listContainer = pagingView.listContainerView
        
        view.bringSubviewToFront(navigationView)
    }
    
    private func initFrame() -> Void {
        pagingView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
        
    }
}

extension JXPagingListContainerView: JXSegmentedViewListContainer {}

extension HWPromoteController:JXPagingViewDelegate {
    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        let vc = HWPromoteListController()
        vc.type = ids[index]
        return vc
    }
    
    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {
        return Int(182*widthScale)
    }
    
    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return topView
    }
    
    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return Int(50*widthScale)
    }
    
    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentedView
    }
    
    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
//    func mainTableViewDidScroll(_ scrollView: UIScrollView) {
//
//    }
}
extension HWPromoteController:JXSegmentedViewDelegate {
    
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}
extension HWPromoteController {
    func loadData() -> Void {
//        pagingView.mainTableView.mj_header?.endRefreshing()
        //        httpManager.getVipHeadProtraitList(typeId: type) { models in
        //            self.tableViewProtocol.models = models
        //            self.tableView.reloadData()
        //
        //            self.tableView.mj_header?.endRefreshing()
        //            self.tableView.mj_footer?.endRefreshing()
        //            if models.count > 0 {
        //                self.modelArr.removeAllObjects()
        //                self.modelArr.addObjects(from: models)
        //            }
        //            self.tableViewProtocol.models = self.modelArr as? [HWHeadPortraitFrameModel]
        //            self.tableView.reloadData()
        //        } failure: { error in
        //            self.tableView.mj_header?.endRefreshing()
        //            self.tableView.mj_footer?.endRefreshing()
        //        }
        httpManager.getGameCategoryInfo() { models in
            self.titles.removeAll()
            self.ids.removeAll()
            for mod in models {
                self.titles.append(mod.name)
                self.ids.append(mod.id)
            }
            self.segmentedDataSource.titles = self.titles
            self.segmentedView.reloadData()
        } failure: { error in

        }
        
        httpManager.getNoticeInfo { infoModels in
            self.topView.models = infoModels
        } failure: { error in

        }

        
        
    }
    
    func addRefresh() -> () {
        
        pagingView.mainTableView.mj_header = MJRefreshNormalHeader()
        pagingView.mainTableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        //        tableView.mj_footer = MJRefreshBackNormalFooter()
        //        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
}

extension HWPromoteController {
    func addAction() -> Void {
        topView.searchButtonClick = {
            self.navigationController?.pushViewController(HWPromoteSearchController(), animated: true)
        }
        
        topView.promoteNoticeButtonClick = {
            let promoteNotice = HWPromotionAnnouncementViewController()
            self.navigationController?.pushViewController(promoteNotice, animated: true)
        }
        
        topView.promoteQRCodeButtonClick = {
            let qrCode = HWPromotionQrCodeViewController()
            self.navigationController?.pushViewController(qrCode, animated: true)
        }
        
        topView.divideDetailButtonClick = {
            let divideDetail = HWDividedIntoViewController()
            self.navigationController?.pushViewController(divideDetail, animated: true)
        }
        
        topView.popularityButtonClick = {
            let rankList = HWRankListController()
            rankList.type = .popularityRank
            self.navigationController?.pushViewController(rankList, animated: true)
        }
        
        topView.didClickBanner = {
            mod in
            if mod.url.count > 0 {
                let webView = HWWebViewController()
                webView.urlString = mod.url
                self.navigationController?.pushViewController(webView, animated: true)
            }
            
        }
    }
}
